package com.house365.core.view.pulltorefresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.house365.core.R;
import com.house365.core.view.pulltorefresh.internal.EmptyViewMethodAccessor;
/**
 * 
 * @modifier Rick Ping
 *
 */
public class PullToRefreshGridView extends PullToRefreshAdapterViewBase<GridView> {

	class InternalGridView extends GridView implements EmptyViewMethodAccessor {

		public InternalGridView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		@Override
		public void setEmptyView(View emptyView) {
			PullToRefreshGridView.this.setEmptyView(emptyView);
		}

		@Override
		public void setEmptyViewInternal(View emptyView) {
			super.setEmptyView(emptyView);
		}

		@Override
		public ContextMenuInfo getContextMenuInfo() {
			return super.getContextMenuInfo();
		}
	}

	public PullToRefreshGridView(Context context) {
		super(context);
		setDisableScrollingWhileRefreshing(false);
	}

	public PullToRefreshGridView(Context context, int mode) {
		super(context, mode);
		setDisableScrollingWhileRefreshing(false);
	}

	public PullToRefreshGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setDisableScrollingWhileRefreshing(false);
	}

	@Override
	protected final GridView createRefreshableView(Context context, AttributeSet attrs) {
		GridView gv = new InternalGridView(context, attrs);
		gv.setId(R.id.gridview);
		return gv;
	}

	@Override
	public ContextMenuInfo getContextMenuInfo() {
		return ((InternalGridView) getRefreshableView()).getContextMenuInfo();
	}
	
	public void onRefreshComplete(CharSequence lastUpdated) {
		mHeaderLayout.setLastUpdate(lastUpdated);
		 super.onRefreshComplete();
		 
	}
	public void setFooterViewVisible(int visibility){
		if(visibility == View.GONE){
			this.removeView(mFooterLayout);
			mCurrentMode = mMode = MODE_PULL_DOWN_TO_REFRESH;
			setPadding(0, -mHeaderHeight, 0,0);
		}else{
			mMode = MODE_BOTH;
			this.addView(mFooterLayout);
			setPadding(0, -mHeaderHeight, 0, -mHeaderHeight);
			
		}
	}
	public void setAdapter(ListAdapter adapter){
		/**
		if(adapter instanceof BaseCacheListAdapter){
			listadapter = (BaseCacheListAdapter)adapter;
			getActureListView().setOnScrollListener(new OnScrollListener() {
				
				@Override
				public void onScrollStateChanged(AbsListView view, int scrollState) {
					switch (scrollState) {
			        case OnScrollListener.SCROLL_STATE_IDLE:
//			        	isScrolling= false;
			        	listadapter.onScrollIdle(getActureListView().getFirstVisiblePosition(), getActureListView().getLastVisiblePosition());
			            break;
			        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
			        	listadapter.setHasFirstScrolled(true);
//			        	listadapter.setIsScrolling(isScrolling);
//			        	isScrolling= true;     
			            break;
			        case OnScrollListener.SCROLL_STATE_FLING:
			        	listadapter.setHasFirstScrolled(true);
//			        	listadapter.setIsScrolling(isScrolling);
//			        	isScrolling= true;
			            break;
					}
					
				}
				
				@Override
				public void onScroll(AbsListView view, int firstVisibleItem,
						int visibleItemCount, int totalItemCount) {
					// TODO Auto-generated method stub
					
				}
			});
		}
		*/
		getRefreshableView().setAdapter(adapter);
	}
	public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
		getRefreshableView().setOnItemClickListener(listener);
	}
	public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener){
		getRefreshableView().setOnItemLongClickListener(listener);
	}
	public void showRefreshView(){
		setHeaderRefreshingInternal(true);
	};
	public GridView getActureListView(){
		return getRefreshableView();
	}
}